<?php
namespace V4;
class C_NewProduct extends \ResourceClass
{
    public $isActive = true;
    public $remark = '最近添加的产品';

    public function get(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require user uid';
        }
        if (!isset($req['userType'])) {
            return 'require user type';
        }
        if (!isset($req['k']) || !isset($req['t'])) {
            return 'data error';
        }
        if (!isset($req['enterpriseId'])) {
            return 'require enterprise id';
        }
        if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $req['enterpriseId'], $req['t'], $req['k']) == false) {
            return 'data illegal';
        }
        if (!isset($req['channelId'])) {
            return 'require channel id';
        }

        $result = array('product_list'=>array());
        $customer_info = \Biz\EnterpriseCustomerHandler::getEnterpriseCustomerById($req['enterpriseId'],$req['uid']);
        $customer_info = self::ormFormatCustomerInfo($customer_info);
        if ($customer_info['channel_id'] != $req['channelId']) {    //验证用户渠道
            return 'invalid channel id ';
        }
        if ($customer_info['enterprise_id'] != $req['enterpriseId']) {//验证用户企业
            return 'invalid enterprise id ';
        }
        $product_handler = new \Biz\ProductHandler();
        $talabtion = new \Cls\Tabulation();
        $talabtion->setLimit(9999);
        $talabtion->setOrderBy('id');
        $product_handler -> getProductArrayByEnterpriseId($req['enterpriseId'],$talabtion);
        $product_list = $talabtion->getData();



        // 获取当前渠道信息
        $channel_cls = \Biz\ChannelHandler::getChannelById($req['channelId']);
        $discount = $channel_cls->getDiscount();
        /**
         * @var $product_cls \Cls\Product\Product
         */
        foreach ($product_list as $product_cls) {
            $product_cls_temp = new \stdClass();
            $product_cls_temp->id = $product_cls->getId();
            $product_cls_temp->enterprise_id = $product_cls->getEnterpriseId();
            $product_cls_temp->brand_id = $product_cls->getBrandId();
            $product_cls_temp->name = $product_cls->getName();
            $product_cls_temp->icon_url = $product_cls->dealIconUrl();
            $product_cls_temp->repertory_warning = $product_cls->getStockWarning();
            $product_cls_temp->individual_packing = $product_cls->getSellUnitString(); // 老版本
            $product_cls_temp->mass_packing = $product_cls->getSellUnitString(); // 老版本
            $product_cls_temp->sell_unit_string = $product_cls->getSellUnitString();
            $product_cls_temp->remark = $product_cls->getDescription();
            $product_cls_temp->insert_time = $product_cls->getCreateTime();
            $product_cls_temp->is_del = $product_cls->getDelStatus();
            $brand_cls = $product_cls->getBrandInfo();
            if ($brand_cls instanceof \Cls\Brand\Brand) {
                $brand_name = $brand_cls->getName();
            } else {
                $brand_name = '未知分类名称';
            }
            $product_cls_temp->brandname = $brand_name;
            $channel_list = $product_cls->getChannelProduct();
            if ($channel_list !== null) {
                $channel_cls = reset($channel_list);
                $product_cls_temp->channel_price = $channel_cls->getPriceRange();
            } else {
                $product_cls_temp->channel_price = 0;
            }
            $product_cls_temp->collect_stock = $product_cls->getRepertoryTotal();
            $product_cls_temp->stock_warning = $product_cls->judgeStockWaring();
            $sku_list = $product_cls->getSkuList();
            $product_cls_temp->sku_info = array();
            if (!empty($sku_list)) {
                /**
                 * @var $sku_cls \Cls\Product\Sku
                 */
                foreach ($sku_list as $sku_cls) {
                    $sku_temp = new \stdClass();
                    $sku_temp->id = $sku_cls->getId();
                    $sku_temp->product_id = $sku_cls->getProductId();
                    $sku_temp->imgurl = $sku_cls->dealImageUrl();
                    $sku_temp->attr = $sku_cls->dealAttr();
                    $sku_temp->code = $sku_cls->getCode();
                    $sku_temp->sellprice = $sku_cls->getChannelSellingPrice($discount);
                    $sku_temp->cost = $sku_cls->getCost();
                    $sku_temp->stock = $sku_cls->getStock();
                    $sku_temp->insert_time = $sku_cls->getCreateTime();
                    $sku_temp->update_time = $sku_cls->getUpdateTime();
                    $product_cls_temp->sku_info [] = $sku_temp;
                }
            }
            $result['product_list'][] = $product_cls_temp;
        }
        /*
         *  促销活动信息
         *  ①筛选该企业下的所有有效活动
         *  ②在活动中筛选出用户所在渠道的活动，即活动的渠道为所有活动或者参与渠道中包含该渠道即可
         *  ③把满足条件的活动参与的产品与查询出的产品对应起来
         *  ④返回活动信息
         */
        $market_biz = new \Biz\MarketHandler();
        $tabulation = new \Cls\Tabulation();
        $tabulation->setLimit(99999);
        $talabtion->setOrderBy('id');
        $tabulation->setWhere('enterprise_id', $req['enterpriseId']);
        $market_biz->getListByEnterpriseId($tabulation);
        if ($tabulation->getSuccessStatus()) {
            $market_list = $tabulation->getData();
            //营销活动的渠道信息
            $market_biz->completeChannelListByMarketList($market_list);
            //完善营销活动中的商品信息
            $market_biz->completeProductListByMarketProductList($market_list);
            /**
             * @var $market_cls \Cls\Market\Market
             */

            foreach ($market_list as $market_cls) {
                //查询当前渠道在不在该活动参与的渠道中
//                    $is_in_channel = ActivityModel::getActivityInChannle($req['channelId'], $market_cls->getId());
                //如果活动参与的渠道类型是所有渠道或者当前用户所在的渠道在参与活动的渠道中
                if ($market_cls->getChannelType() == \Cls\Market\Market::CHANNEL_ALL || $market_cls->judgeInChannel($req['channelId'])) {
                    //获取该活动参与的所有产品
                    $market_product_list = $market_cls->getProductList();
                    //取出所有产品的id组成id集合
                    $product_ids = array();
                    /**
                     * @var $market_product_cls \Cls\Market\Product
                     */
                    foreach ($market_product_list as $market_product_cls) {
                        $product_ids[] = $market_product_cls->getProductId();
                    }
                    //如果参与活动产品类型为所有产品或者该产品在参与的活动中

                    foreach ($result['product_list'] as $key => $product) {
                        $result['product_list'][$key]->activity_info = isset($result['product_list'][$key]->activity_info) ? $result['product_list'][$key]->activity_info : array();
                        if ($market_cls->getProductType() == \Cls\Market\Market::PRODUCT_ALL || in_array($product->id, $product_ids)) {
                            $market_cls_temp = new \stdClass();
                            $market_cls_temp->id = $market_cls->getId();
                            $market_cls_temp->title = $market_cls->getTitle();
                            $market_cls_temp->type = $market_cls->getType();
                            $market_cls_temp->condition = $market_cls->getCondition();
                            $market_cls_temp->discount = $market_cls->getDicount();
                            $market_cls_temp->rewards = $market_cls->getRewards();
                            $market_cls_temp->product_type = $market_cls->getProductType();
                            $market_cls_temp->channel_type = $market_cls->getChannelType();
                            $market_cls_temp->start_time = $market_cls->getStartTime();
                            $market_cls_temp->end_time = $market_cls->getEndTime();
                            $market_cls_temp->status = $market_cls->getStatus();
                            $market_cls_temp->enterprise_id = $market_cls->getEnterpriseId();
                            $market_cls_temp->unit = $market_cls->getUnit();
                            $market_cls_temp->create_time = $market_cls->getCreateAt();
                            $market_cls_temp->edit_time = $market_cls->getUpdateAt();
                            $market_cls_temp->update_time = $market_cls->getUpdateAt();
                            array_push($result['product_list'][$key]->activity_info, $market_cls_temp);
                        }
                    }
                }
            }
        } else {
            foreach ($result['product_list'] as $key => $product) {
                $result['product_list'][$key]->activity_info = array();
            }
        }
    }
    //拼装客户信息
    public static function ormFormatCustomerInfo($customer_info)
    {
        $info['id'] = $customer_info->getId();
        $info['account'] = $customer_info->getAccount();
        $info['pwd'] = $customer_info->getPassword();
        $info['name'] = '';
        $info['company'] = $customer_info->getCompany();
        $info['contact'] = $customer_info->getContact();
        $info['admin_id'] = $customer_info->getAdminId();
        $info['department'] = '';
        $info['customer_code'] = $customer_info->getCustomerCode();
        $info['tel'] = $customer_info->getTelephone();
        $info['channel_id'] = $customer_info->getChannelId();
        $info['enterprise_id'] = $customer_info->getEnterpriseId();
        $info['enterprise_role_id'] = '';
        $info['warehouse_id'] = $customer_info->getWarehouseId();
        $info['type'] = $customer_info->getType();
        $info['remark'] = $customer_info->getRemark();
        $info['is_del'] = $customer_info->getDelStatus();
        $info['is_use'] = $customer_info->getUseStatus();
        $info['insert_time'] = $customer_info->getCreateTime();
        $info['update_time'] = $customer_info->getUpdateTime();
        return $info;
    }
}